// 获取应用实例
const app = getApp()
import user from '../../utils/user'
import {
    getUserInfoByUserId,
    joinProject,
    setProjectName,
    workInvitationLeader,
    switchUserCompany
} from '../../api/app'
Page({
    data: {
        tabs: []
    },
    dateChange () {

    },
    onShow () {

    },
    onLoad (option) {
        console.log(option, 'option')
        if (option.scene) {
            let scene = decodeURIComponent(option.scene)
            scene = scene.split('&')
            for (const i in scene) {
                const query = scene[i].split('=')
                option[query[0]] = query[1]
            }
            option.userId = option.uId
            option.companyId = option.cId
            option.projectId = option.pId
            option.createUserId = option.cUId
            option.jobId = option.jId
            console.log(option.createUserId, option.jobId, 'createUserId')
        }
        // 招聘岗位邀请
        if ((option.createUserId && option.jobId) || (option.userId && option.companyId)) {
            user.waitLogin().then(() => {
                getUserInfoByUserId({
                    userId: option.createUserId,
                    companyId: option.companyId
                }).then(res => {
                    if (!res[0]) {
                        app.toast('未找到该用户')
                        return 0
                    }
                    option.userName = res[0].userName
                    option.companyName = res[0].companyName
                    const isCompany = (user.companyIdList.length > 0 && user.companyIdList.indexOf(Number(option.companyId)) !== -1)
                    if(!isCompany){
                        this.setData({
                            inviteRegisterStatus: true,
                            invite: option
                        })
                    }
                    // 招聘驻场邀请
                    if (option.createUserId && option.jobId) {
                        if(option.companyId && option.companyId !== user.companyId){
                            switchUserCompany({
                                companyId: option.companyId,
                                userId: user.userId
                            }).then(() => {
                                // 不在当前公司 切换公司
                                this.joinWork(option)
                            })
                        }else{
                            // 已在当前公司 直接进入
                            // 在公司加入该岗位 管理列表
                            this.joinWork(option)
                        }
                    }
                    // 发薪驻场邀请
                    else if (option.userId && option.companyId) {
                        // 不需要加入项目时提示，已在该公司
                        if (!option.projectId) {
                            app.alert('您已加入该公司，请不要重复加入')
                        } else {
                            this.setData({
                                invite: option
                            })
                            this.registerComplete()
                        }
                    }
                })
            }, () => {
                // 从未注册过的人 先被邀请进入公司 在进入驻厂
                console.log('你没有注册')
                this.setData({
                    inviteRegisterStatus: true,
                    invite: option
                })
            })
        }
        else {
            user.waitLogin().then(() => {
                if (!user.hasPassword) {
                    app.confirm('您还没有为该公司设置验证密码，是否立即前往？').then(() => {
                        wx.navigateTo({
                            url: '/pages/password/password?type=set&oldPass='
                        })
                    })
                }
            })
        }
    },
    switchtab (event) {
        if (!user.userId) {
            app.confirm('您还未登录，是否立即登录？').then(() => {
                user.goRegister()
            })
            return
        }
        this.setData({
            current: event.currentTarget.dataset.index,
            pageNum: 1,
            // hasNext: true
        })
    },
    // 加入 管理招聘企业
    joinWork (option) {
        console.log(option, 'option')
        workInvitationLeader({
            jobId: option.jobId,
            createUserId: option.createUserId
        }).then(() => {
            app.toast('您已成功加入该招聘岗位')
        })
    },
    goNotice () {
        wx.navigateTo({
            url: '/packagePay/pages/bill/bill'
        })
    },
    onFastPay () {
        if (user.quickProject) {
            wx.navigateTo({
                url: '/packagePay/pages/fast-pay/fast-pay'
            })
        }
        else {
            wx.navigateTo({
                url: '/packagePay/pages/fast-pay-guide/fast-pay-guide'
            })
        }

    },
    setProjectName (event) {
        this.setData({
            setProjectNameId: event.detail,
            setProjectNameIndex: event.currentTarget.dataset.index
        })
    },
    onCancelProjectName () {
        this.setData({
            setProjectNameId: false,
            setProjectNameIndex: false
        })
    },
    onSetProjectName (event) {
        if (!event.detail.name) {
            app.toast('请输入项目名称')
            return
        }
        setProjectName({
            id: this.data.setProjectNameId,
            projectName: event.detail.name
        }).then(() => {
            app.toast('修改成功', 'success')
            this.setData({
                setProjectNameId: false,
                ['projectList[' + this.data.setProjectNameIndex + '].projectName']: event.detail.name
            })
        })
    },

    // 注册
    inviteRegister (option) {
        getUserInfoByUserId({
            userId: option.userId
        }).then(res => {
            if (!res[0]) {
                app.toast('未找到该用户')
                return 0
            }
            option.userName = res[0].userName
            option.companyName = res[0].params.companyName
            option.companyId = res[0].companyId
            this.setData({
                inviteRegisterStatus: true,
                invite: option
            })
        })
    },
    // 注册成功
    registerComplete () {
        this.setData({
            companyName: user.companyName,
            inviteRegisterStatus: false
        })
        user.login().then(user => {
            this.setData({
                'companyName': user.companyName
            })
            if (this.data.invite.projectId) {
                this.joinProject(this.data.invite)
            } else {
                user.reLogin().then(() => {
                    wx.reLaunch({
                        url: '/pages/index/index',
                    })
                })
            }
        })
    },
    onScroll () {
        console.log(this.data.hasNext, 'hasNext')
        if (!this.data.hasNext) {
            return
        }
        this.setData({
            pageNum: this.data.pageNum + 1
        })
        this.getProjectList()
    },
    hideAddProject () {
        this.setData({
            addProjectStatus: false
        })
    },
    showAddProject () {
        if (!user.userId) {
            app.confirm('您还未登录，是否立即登录？').then(() => {
                user.goRegister()
            })
            return 0
        }
        this.setData({
            addProjectStatus: true
        })
    },
    completeSubscription () {
        this.setData({
            showSubscription: false
        })
    },
    // 加入团队
    joinProject(option) {
        user.reLogin().then(() => {
            joinProject({
                userId: user.userId,
                projectId: option.projectId,
                createUserId: option.userId
            }).then(() => {
                app.toast('加入项目成功！', 'success')
                setTimeout(() => {
                    this.getProjectList()
                    if (!user.hasPassword) {
                        app.confirm('您还没有为该公司设置验证密码，是否立即前往？').then(() => {
                            wx.navigateTo({
                                url: '/pages/password/password?type=set&oldPass='
                            })
                        })
                    }
                }, 1500)
            })
        })
    
    },
    onShareAppMessage () {
        return {
            title: user.userName + '邀请您一起使用子弹发薪|无卡发薪快一步',
            imageUrl: '/assets/img/home/share.png'
        }
    },
    onShareTimeline () {
        return {
            title: '我在使用【子弹发薪 | 无卡发薪快一步】',
            imageUrl: '/assets/img/home/share.png'
        }
    },
    showVideo () {
        this.setData({
            videoStatus: true
        })
    },
})